using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Core;
using System.Reflection;
using SPHelpCollectionBuilder.Icons;
using System.Text.RegularExpressions;

namespace SPHelpCollectionBuilder
{
    public partial class HelpItemTypeChooser : Form
    {
        private HelpItem _helpItem = null;

        public HelpItem HelpItem
        {
            get { return _helpItem; }
            set { _helpItem = value; }
        }

        private string _helpItemId = string.Empty;

        public string HelpItemId
        {
            get { return _helpItemId; }
            set { _helpItemId = value; }
        }

        private ProjectTree _projectTree = null;

        public ProjectTree ProjectTree
        {
            get { return _projectTree; }
            set { _projectTree = value; }
        }

        public HelpItemTypeChooser()
        {
            InitializeComponent();

            Assembly assem = Assembly.GetExecutingAssembly();
            pictureBoxHelpCategoryIcon.Image = Image.FromStream(assem.GetManifestResourceStream(typeof(Dummy), "helpCategory.png"));
            pictureBoxHelpContextIcon.Image = Image.FromStream(assem.GetManifestResourceStream(typeof(Dummy), "helpContext.png"));
            pictureBoxHelpCssIcon.Image = Image.FromStream(assem.GetManifestResourceStream(typeof(Dummy), "helpCss.png"));
            pictureBoxHelpImageIcon.Image = Image.FromStream(assem.GetManifestResourceStream(typeof(Dummy), "helpImage.png"));
            pictureBoxHelpScriptIcon.Image = Image.FromStream(assem.GetManifestResourceStream(typeof(Dummy), "helpScript.png"));
            pictureBoxHelpTopicIcon.Image = Image.FromStream(assem.GetManifestResourceStream(typeof(Dummy), "helpTopic.png"));
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (radioButtonHelpCategory.Checked)
                HelpItem = new HelpCategory();
            else if (radioButtonHelpContext.Checked)
                HelpItem = new HelpContext();
            else if (radioButtonHelpCss.Checked)
                HelpItem = new HelpCss();
            else if (radioButtonHelpImage.Checked)
                HelpItem = new HelpImage();
            else if (radioButtonHelpScript.Checked)
                HelpItem = new HelpScript();
            else if (radioButtonHelpTopic.Checked)
                HelpItem = new HelpTopic();
            else
            {
                MessageBox.Show("Choose a help item type or cancel", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string id = TidyId(textBoxId.Text);

            if(string.IsNullOrEmpty(textBoxId.Text))
            {
                MessageBox.Show("Enter a valid Id", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            id = string.Concat(HCProjectManager.CurrentProject.HCId, ".", id);
            id = id.ToUpper();
            HelpItemId = id;

            //make sure there isn't another help item with the same id in the tree
            TreeNode find = ProjectTree.FindNodeById(HelpItemId, true);
            if (find != null)
            {
                MessageBox.Show("There is already another help item with this id, choose a different id", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(HelpItemId))
            {
                MessageBox.Show("Enter an Id for the help item");
                return;
            }

            this.DialogResult = DialogResult.OK;
        }

        private string TidyId(string inputId)
        {
            /*
             * .check see whether input id has non-alphanumerica characters
             * .if so, tidy it up and warn user
             */
            string result = inputId;

            MatchCollection matches = Regex.Matches(inputId, "\\W");
            if (matches != null && matches.Count > 0)
            {
                result = Helper.TidyStringOnlyKeepAlphanumeric(inputId);
                MessageBox.Show("Non-alphanumeric characters will be removed from the id", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            return result;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void pictureBoxHelpCategoryIcon_Click(object sender, EventArgs e)
        {
            radioButtonHelpCategory.Select();
        }

        private void pictureBoxHelpTopicIcon_Click(object sender, EventArgs e)
        {
            radioButtonHelpTopic.Select();
        }

        private void pictureBoxHelpContextIcon_Click(object sender, EventArgs e)
        {
            radioButtonHelpContext.Select();
        }

        private void pictureBoxHelpImageIcon_Click(object sender, EventArgs e)
        {
            radioButtonHelpImage.Select();
        }

        private void pictureBoxHelpCssIcon_Click(object sender, EventArgs e)
        {
            radioButtonHelpCss.Select();
        }

        private void pictureBoxHelpScriptIcon_Click(object sender, EventArgs e)
        {
            radioButtonHelpScript.Select();
        }
    }
}